<template>
	<view class="video-list-container">
		<view class="header">
			<view class="title">精选视频</view>
		</view>
		
		<scroll-view scroll-y="true" class="video-list" @scrolltolower="loadMore">
			<view v-if="loading" class="loading">
				<uni-load-more status="loading"></uni-load-more>
			</view>
			
			<view v-else-if="videoList.length === 0" class="empty-tip">
				暂无视频内容
			</view>
			
			<view v-else v-for="(item, index) in videoList" :key="index" class="video-item" @click="goToDetail(item.videoId)">
				<view class="video-cover">
					<image :src="item.coverUrl" mode="aspectFill"></image>
					<view class="video-duration">
						<uni-icons type="videocam" size="14" color="#FFFFFF"></uni-icons>
					</view>
				</view>
				<view class="video-info">
					<view class="video-title">{{item.title}}</view>
					<view class="video-meta">
						<view class="location">
							<uni-icons type="location" size="14" color="#999999"></uni-icons>
							<text>{{item.location}}</text>
						</view>
						<view class="stats">
							<view class="likes">
								<uni-icons type="heart" size="14" color="#999999"></uni-icons>
								<text>{{item.likes}}</text>
							</view>
							<view class="comments">
								<uni-icons type="chatbubble" size="14" color="#999999"></uni-icons>
								<text>{{item.commentsCount}}</text>
							</view>
						</view>
					</view>
					<view class="video-tags">
						<text v-for="(tag, tagIndex) in formatTags(item.tags)" :key="tagIndex" class="tag">{{tag}}</text>
					</view>
				</view>
			</view>
			
			<view v-if="videoList.length > 0 && hasNextPage" class="load-more">
				<uni-load-more status="loading"></uni-load-more>
			</view>
			
			<view v-if="videoList.length > 0 && !hasNextPage" class="no-more">
				<uni-load-more status="noMore" :contentText="{ contentnomore: '没有更多了' }"></uni-load-more>
			</view>
		</scroll-view>
	</view>
</template>

<script>
import { getVideoList } from '@/api/video.js';

export default {
	data() {
		return {
			videoList: [],
			page: 1,
			size: 1,
			loading: true,
			hasNextPage: false,
			totalPages: 0
		};
	},
	onLoad() {
		this.loadVideoList();
	},
	methods: {
		// 加载视频列表
		async loadVideoList() {
			this.loading = true;
			try {
				const res = await getVideoList(this.page, this.size);
				if (res.code === 200) {
					const pageInfo = res.data;
					this.videoList = [...this.videoList, ...pageInfo.list];
					this.hasNextPage = pageInfo.hasNextPage;
					this.totalPages = pageInfo.pages;
					console.log('视频列表数据:', this.videoList);
				} else {
					uni.showToast({
						title: res.errorMessage || '获取视频列表失败',
						icon: 'none'
					});
				}
			} catch (error) {
				console.error('获取视频列表出错:', error);
				uni.showToast({
					title: '获取视频列表失败',
					icon: 'none'
				});
			} finally {
				this.loading = false;
			}
		},
		
		// 加载更多
		loadMore() {
			if (this.hasNextPage && !this.loading) {
				this.page++;
				this.loadVideoList();
			}
		},
		
		// 跳转到视频详情页
		goToDetail(videoId) {
			uni.navigateTo({
				url: `/pages/video/detail?videoId=${videoId}`
			});
		},
		
		// 格式化标签
		formatTags(tagsString) {
			if (!tagsString) return [];
			return tagsString.split(',').map(tag => tag.trim());
		}
	}
};
</script>

<style lang="scss" scoped>
.video-list-container {
	display: flex;
	flex-direction: column;
	height: 100vh;
	background-color: #f5f5f5;
	
	.header {
		padding: 20rpx 30rpx;
		background-color: #ffffff;
		
		.title {
			font-size: 36rpx;
			font-weight: bold;
			color: #333333;
		}
	}
	
	.video-list {
		flex: 1;
		padding: 20rpx;
		
		.video-item {
			margin-bottom: 30rpx;
			background-color: #ffffff;
			border-radius: 12rpx;
			overflow: hidden;
			box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
			
			.video-cover {
				position: relative;
				width: 100%;
				height: 360rpx;
				
				image {
					width: 100%;
					height: 100%;
				}
				
				.video-duration {
					position: absolute;
					right: 20rpx;
					bottom: 20rpx;
					padding: 6rpx 12rpx;
					background-color: rgba(0, 0, 0, 0.5);
					border-radius: 10rpx;
					color: #ffffff;
					font-size: 24rpx;
					display: flex;
					align-items: center;
				}
			}
			
			.video-info {
				padding: 20rpx;
				
				.video-title {
					font-size: 32rpx;
					font-weight: bold;
					color: #333333;
					margin-bottom: 15rpx;
				}
				
				.video-meta {
					display: flex;
					justify-content: space-between;
					margin-bottom: 15rpx;
					
					.location {
						display: flex;
						align-items: center;
						font-size: 24rpx;
						color: #999999;
						
						text {
							margin-left: 6rpx;
						}
					}
					
					.stats {
						display: flex;
						
						.likes, .comments {
							display: flex;
							align-items: center;
							font-size: 24rpx;
							color: #999999;
							margin-left: 20rpx;
							
							text {
								margin-left: 6rpx;
							}
						}
					}
				}
				
				.video-tags {
					display: flex;
					flex-wrap: wrap;
					
					.tag {
						padding: 4rpx 12rpx;
						background-color: #f0f0f0;
						color: #666666;
						font-size: 22rpx;
						border-radius: 6rpx;
						margin-right: 10rpx;
						margin-bottom: 10rpx;
					}
				}
			}
		}
		
		.loading, .empty-tip, .load-more, .no-more {
			padding: 30rpx 0;
			text-align: center;
		}
		
		.empty-tip {
			color: #999999;
			font-size: 28rpx;
		}
	}
}
</style> 